home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / prog_disc / volume_3 / issue04 / c / c_cards < prev   
Encoding:
Text File  |  1989-12-11  |  2.5 KB  |  158 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef int Card;
  5.  
  6. typedef int Suit;
  7. typedef int Rank;
  8.  
  9. #define CLUBS 1
  10. #define DIAMONDS 2
  11. #define SPADES 3
  12. #define HEARTS 4
  13. #define FIRST_SUIT 1
  14. #define LAST_SUIT 4
  15.  
  16. Card make_card( Suit suit, Rank rank )
  17.     {
  18.     return (suit << 4) + rank;
  19.     }
  20.  
  21. Suit card_suit( Card c )
  22.     {
  23.     return c >> 4;
  24.     }
  25.  
  26.  Rank card_rank( Card c )
  27.     {
  28.     return c & 0xf;
  29.     }
  30.  
  31.  Card pack[52];
  32.  
  33. void initialise_pack(void)
  34.     {
  35.     Suit s;
  36.     Rank r;
  37.     int i = 0;
  38.     for (s = FIRST_SUIT; s <= LAST_SUIT; s++)
  39.         for (r = 1; r < 14; r++)
  40.             pack[ i++ ] = make_card( s, r );
  41.     }
  42.  
  43. void shuffle_pack(void)
  44.     {
  45.     int i;
  46.     for (i = 51; i != 0; i--)
  47.         {
  48.         int j = rand() % (i + 1);
  49.         if (i != j)
  50.             {
  51.             Card c = pack[i];
  52.             pack[i] = pack[j];
  53.             pack[j] = c;
  54.             }
  55.         }
  56.     }
  57.  
  58. char *ranks = "?A23456789TJQK";
  59.  
  60.  
  61. char *suits[] = 
  62.     {
  63.     "(no suit)",
  64.     "clubs",
  65.     "diamonds",
  66.     "spades",
  67.     "hearts"
  68.     };
  69.  
  70. void print_card( Card c )
  71.     {
  72.     printf
  73.         ( 
  74.         "%c of %s", 
  75.         ranks[ card_rank( c ) ], 
  76.         suits[ card_suit( c ) ]
  77.         );
  78.     }
  79.  
  80. void print_pack(void)
  81.     {
  82.     int i;
  83.     for (i = 0; i < 52; i++) 
  84.         {
  85.         print_card( pack[i] );
  86.         printf( "\n" );
  87.         }
  88.     }
  89.  
  90. typedef struct
  91.     {
  92.     int length;
  93.     Card cards[13];
  94.     }
  95.     Holding;
  96.  
  97. typedef Holding Hand;
  98.  
  99. void clear_hand( Hand *h );
  100.  
  101. void give_card( Hand *h, Card c );
  102.  
  103. void print_hand( Hand *h );
  104.  
  105. #define PLAYERS 4
  106.  
  107. Hand players[PLAYERS];
  108.  
  109. void deal_cards(void)
  110.     {
  111.     int i = 0, player, card;
  112.     for (player = 0; player < PLAYERS; player++)
  113.         for (card = 0; card < 13; card++)
  114.             give_card( &players[player], pack[i++] );
  115.     }
  116.  
  117. void print_hands(void)
  118.     {
  119.     int player;
  120.     for (player = 0; player < PLAYERS; player++)
  121.         {
  122.         printf( "Hand for player %d\n", player );
  123.         print_hand( &players[player] );
  124.         }
  125.     }
  126.  
  127. void clear_hand( Hand *hand )
  128.     {
  129.     (*hand).length = 0;
  130.     }
  131.  
  132. void give_card( Hand *hand, Card c )
  133.     {
  134.     hand->cards[ hand->length++ ] = c;
  135.     }
  136.  
  137. void print_hand( Hand *hand )
  138.     {
  139.     int i = hand->length;
  140.     while (i > 0) 
  141.         {
  142.         print_card( hand->cards[--i] );
  143.         printf( "\n" );
  144.         }
  145.     }
  146.  
  147. int main( int argc, char *argv[] )
  148.     {
  149.     initialise_pack();
  150.     shuffle_pack();
  151.     printf( "Shuffled pack:\n" );
  152.     print_pack();
  153.     deal_cards();
  154.     printf( "Dealt hands:\n" );
  155.     print_hands();
  156.     }
  157.  
  158.